<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>02_受控组件</title>
    <link rel="shortcut icon" href="../../favicon.ico">
</head>
<body>
<div id="test"></div>

<!--引入react核心库-->
<script src="../js/react.development.js"></script>
<!--引入react-dom，用于支持react操作DOM-->
<script src="../js/react-dom.development.js"></script>
<!--引入babel，用于将jsx转为js-->
<script src="../js/babel.min.js"></script>

<script type="text/babel">
    // 创建组件
    class Login extends React.Component {
        handleSubmit = (event) => {
            event.preventDefault();
            const {username, password} = this.state;
            alert(`你输入的用户是：${username},你输入的密码是：${password}`)
        }
        // 初始化状态
        state = {
            username: '',
            password: ''
        }

        saveUsername = (event) => {
            this.setState({username: event.target.value}, () => {
            })
        }
        savePassword = (event) => {
            this.setState({password: event.target.value}, () => {
            })
        }

        render() {
            return (
                <form onSubmit={this.handleSubmit}>
                    <label>
                        用户名：
                        <input type="text" onChange={this.saveUsername} name="username"/>
                    </label>
                    <label>
                        密码：
                        <input type="password" onChange={this.savePassword} name="password"/>
                    </label>
                    <button>登录</button>
                </form>
            )
        }
    }

    ReactDOM.render(<Login/>, document.getElementById('test'));
</script>
</body>
</html>